import requests
import re
result = requests.get('https://www.qschina.cn/sites/default/files/qs-rankings-data/cn/2005378.txt')

# # print(result.text.encode('utf-8').decode('unicode_escape'))

# f = open('./23/results.txt','w',encoding='utf-8')
# f.write(result.text.encode('utf-8').decode('unicode_escape'))
# f.closed

# # print(type(result.json()))
# print(type(result.json()['data'][0]['score']))
# print(result.json()['data'][0]['score'])

# str = '\u65af\u5766\u798f\u5927\u5b66'
# print(str)
# print(str.encode().decode())

def getTitle(content):
    pattern = re.compile('<a.*?>(.*?)<\\/a>')
    result = re.search(pattern, content)
    return result.group(1)

import csv

with open('ranking.csv', 'w', newline='',encoding='utf-8') as csvfile:
    fieldnames = ['国家','地区','大学','得分','排名']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    for item in result.json()['data']:
        writer.writerow({'国家': item['country'],'地区':item['region'],'大学':getTitle(item['title']),'得分':item['score'],'排名':item['rank_display']})
